/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.entityGenerator.base.dataTypeData.dataTypePropGen.assoUdt;

import com.inspur.edp.caf.generator.BaseGenerator;
import com.inspur.edp.caf.generator.baseInfo.AnnotationInfo;
import com.inspur.edp.caf.generator.baseInfo.AnnotationType;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.cef.api.attr.EnrichedAssoUdtAttribute;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.generator.entityGenerator.base.dataTypeData.DataTypeDataGeneratorContext;
import com.inspur.edp.cef.generator.entityGenerator.base.dataTypeData.dataTypePropGen.UdtDataPropertyGenerator;
import com.inspur.edp.udt.designtime.api.entity.SimpleDataTypeDef;
import java.util.HashMap;
import lombok.var;

import java.util.ArrayList;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.TypeLiteral;

public class AssoUdtDataPropertyGenerator extends UdtDataPropertyGenerator {

  @Override
  protected SimpleDataTypeDef getUdt() {
    return (SimpleDataTypeDef) super.getUdt();
  }

  public AssoUdtDataPropertyGenerator(
          IGspCommonField field, SimpleDataTypeDef udt, DataTypeDataGeneratorContext dataGenContext) {
    super(field, udt, dataGenContext);
  }

  @Override
  protected TypeInfo getPropertyType() {
    return new TypeInfo(AssoUdtEnrichedDataGenerator.getFullItemName(field));
  }

  @Override
  protected ArrayList<BaseGenerator> createChildGenerators() {

    ArrayList list =super.createChildGenerators();
    if (list == null){
      list = new ArrayList<BaseGenerator>();
    }
    // IXXXUdtData子类
    list.add(new AssoUdtEnrichedDataGenerator(field, getUdt(), dataGenContext));
    // XXXInfo子类
    list.add(new AssoUdtEnrichedInfoGenerator(field, getUdt(), dataGenContext));
    return list;
  }

  @Override
  protected ArrayList<AnnotationInfo> getGetterAttributeList() {
    var type = super.getPropertyType();

    var rez =super.getGetterAttributeList();
    if (rez == null) {
      rez = new ArrayList<AnnotationInfo>();
    }

    AnnotationInfo info = new AnnotationInfo();
    info.setAnnotationType(AnnotationType.Normal);
    TypeInfo typeInfo = new TypeInfo(EnrichedAssoUdtAttribute.class);
//    typeInfo.setTypePackageName("com.inspur.edp.cef.api.attr");
    info.setTypeInfo(typeInfo);
    TypeLiteral param = ast.newTypeLiteral();
    param.setType(ast.newSimpleType(ast.newName(type.getTypeName())));
    HashMap<String, Expression> annoParams = new HashMap<String, Expression> ();
    annoParams.put("assoUdtDataType", param);
    info.setParams(annoParams );
    rez.add(info);

    return rez;
  }

  @Override
  protected void beforeGenerate() {
    super.beforeGenerate();
    propInfo.setExtendInfo("EnrichedAssoUdtAttribute", super.getPropertyType().getTypeName());
  }
}
